﻿using psafth.FSR.Lapcounter.AMB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P3Tester
{
    class Message
    {
        /// <summary>
        /// Version (default = 02)
        /// </summary>
        public byte Version
        {
            get;
            private set;
        }

        /// <summary>
        /// Length of record LSB
        /// </summary>
        public byte LengthLSB
        {
            get;
            private set;
        }

        /// <summary>
        /// Length of record MSB
        /// </summary>
        public byte LengthMSB
        {
            get;
            private set;
        }

        /// <summary>
        /// CRC of record LSB
        /// </summary>
        public byte ChecksumLSB
        {
            get;
            private set;
        }

        /// <summary>
        /// CRC of record MSB
        /// </summary>
        public byte ChecksumMSB
        {
            get;
            private set;
        }

        /// <summary>
        /// Flags of record LSB
        /// </summary>
        public byte FlagsLSB
        {
            get;
            private set;
        }

        /// <summary>
        /// Flags of record MSB
        /// </summary>
        public byte FlagsMSB
        {
            get;
            private set;
        }

        /// <summary>
        /// TOM (Type of Message) LSB
        /// </summary>
        public byte TypeLSB
        {
            get;
            private set;
        }

        /// <summary>
        /// TOM (Type of Message) MSB
        /// </summary>
        public byte TypeMSB
        {
            get;
            private set;
        }

        /// <summary>
        /// Fields of record
        /// </summary>
        public List<byte[]> Record
        {
            get;
            private set;
        }


        private const int ESC = 0x8d; // Escape character
        private const int ESCx = 0x20; // value to subtract from escaped char
        private const int SOR = 0x8e; // Start of Record
        private const int EOR = 0x8f; // End of Record


        public Message(byte[] ba)
        {
            this.Version = ba[0x01];                  // 01 - Version (default = 02)
            this.LengthLSB = ba[0x02];                // 02 - length of record LSB
            this.LengthMSB = ba[0x03];                // 03 - length of record MSB
            this.ChecksumLSB = ba[0x04];              // 04 - CRC of record LSB
            this.ChecksumMSB = ba[0x05];              // 05 - CRC of record MSB
            this.FlagsLSB = ba[0x06];                 // 06 - Flags of record LSB
            this.FlagsMSB = ba[0x07];                 // 07 - Flags of record MSB
            this.TypeLSB = ba[0x08];                  // 08 - TOR (Type of Message) LSB
            this.TypeMSB = ba[0x09];                  // 09 - TOR (Type of Message) MSB

            this.Record = new List<byte[]>();

            List<byte> dataline = new List<byte>();

            for (int i = 0x0a; i < ba.Length - 1; i++)
            {
                if (ba[i] != EOR)
                {
                    int lor = ba[i + 1];

                    for (int j = i; j < i + lor + 2; j++)
                    {
                        dataline.Add(ba[j]);
                    }

                    Record.Add(dataline.ToArray());

                    i += lor + 1;

                    dataline.Clear();

                }
            }

            // 10 - FORs (Fields of Record)
            // ...
            // ...
            // EOF (End of Record = 8f)
        }
    }
}
